
/****************************************************************************************/
*Save data files to folder. There are data files for 
*		- Firm-level analysis (Tables 1a, 2,3, and 6)
*		- Establishement-level analysis (Tables 1b and 5)
*		- Event level analysis (Table 4)


/****************************************************************************************/
clear
global path D:\folder  /*Set path where data is saved.  Will also be where graphs are output*/



/****************************************************************************************/
*Firm-level analysis
/****************************************************************************************/


use  "$path\firm_data.dta", replace




***Sample Set
sort permno fyear
gen in_sample=1 if !mi(wmarkettobook) & !mi(wRegIn_Regulations) & !mi(l.wbookleverage) &  !mi(l.wprofit) & !mi(l.wtang) & !mi(l.wcapx) &  !mi(l.wannualret) & !mi(l.wlnsize) 

	
		
/*Table 1 1 Panel A*/
***Summary Stats		
quietly{
local sumvars wRegIn_Regulations  wmarkettobook  wbookleverage  wprofit wtang wcapx  wannualret wlnsize wrevolve connected_all connected_maj wrevolve agency_head_re wPCM_compu wmarkup_ebit 
eststo clear
estpost tabstat `sumvars' if bookequity>0 & in_sample==1 ,  stat(mean p50 sd p10 p90) columns(statistics)
eststo total
}

esttab total, cells("mean(fmt(a2) label(Mean)) p50(fmt(a2) label(Median)) sd(fmt(a2) label(Std. Dev))")



/*Table 2*/
quietly{
sort permno fyear
eststo clear
eststo: reg wmarkettobook wRegIn_Regulations l.wbookleverage  l.wprofit l.wtang l.wcapx  l.wannualret l.wlnsize  if bookequity>0 ,  cluster(permno)
estadd local firmfe "No"
estadd local yearfe "No"

eststo: reg wmarkettobook  wRegIn_Regulations l.wbookleverage  l.wprofit l.wtang l.wcapx  l.wannualret l.wlnsize  i.yr if  bookequity>0 ,  cluster(permno)
estadd local firmfe "No"
estadd local yearfe "Yes"


eststo: xtreg wmarkettobook wRegIn_Regulations  l.wbookleverage  l.wprofit l.wtang l.wcapx  l.wannualret l.wlnsize  i.yr if  bookequity>0 , fe cluster(permno)
estadd local firmfe "Yes"
estadd local yearfe "Yes"
}

esttab, beta label drop(*.yr)



/*Table 3*/

quietly{
eststo clear
eststo t0: areg wmarkettobook 1.highrest#c.connected_all 1.highrest  c.connected_all  wbookleverage  l.wprofit l.wtang l.wcapx  l.wlnsize  i.yr if  bookequity>0 ,  a(ind_dum) cluster(permno)


eststo t1: areg wmarkettobook 1.highrest#c.connected_maj 1.highrest c.connected_maj  wbookleverage  l.wprofit l.wtang l.wcapx   l.wlnsize  i.yr  if  bookequity>0 , a(ind_dum)    cluster(permno)


eststo t6: areg wmarkettobook 1.highrest#c.connected_all 1.highrest c.connected_all  wbookleverage  l.wprofit l.wtang l.wcapx  l.wlnsize  i.yr if  bookequity>0 ,  a(permno) cluster(permno)

eststo t7: areg wmarkettobook 1.highrest#c.connected_maj 1.highrest c.connected_maj  wbookleverage  l.wprofit l.wtang l.wcapx   l.wlnsize  i.yr  if  bookequity>0 , a(permno)    cluster(permno)


eststo t8: areg wmarkettobook 1.highrest#c.connected_all 1.highrest c.connected_all  wbookleverage  l.wprofit l.wtang l.wcapx  l.wlnsize  i.yr i.state_dum if  bookequity>0 ,  a(permno) cluster(permno)


eststo t9: areg wmarkettobook 1.highrest#c.connected_maj 1.highrest c.connected_maj  wbookleverage  l.wprofit l.wtang l.wcapx   l.wlnsize  i.yr i.state_dum  if  bookequity>0 , a(permno)    cluster(permno)

estadd local stateyearfe "No"




estadd local yearfe "Yes", replace: t0 t1 t6 t7 t8 t9

estadd local indfe "Yes", replace: t0 t1 
estadd local indfe "No", replace:  t6 t7  t8 t9

estadd local statefe "Yes", replace:  t8 t9
estadd local statefe "No", replace: t0 t1   t6 t7  


estadd local firmfe "Yes", replace: t6 t7 t8 t9
estadd local firmfe "No", replace: t0 t1

}

esttab  t0  t1 t6 t7 t8 t9, ar2  drop(*.yr *.state_dum  _cons) n stats(yearfe indfe firmfe statefe, fmt(0 0  0 0 0) labels("Year" "Industry""Firm" "Stat" ))



/*Figure 1*/
foreach v of varlist highrevolve highhead  {
	preserve
		quietly{
			cap drop highrest
			qui sum wRegIn_Regulations,d
			gen highrest=wRegIn_Regulations>`r(p50)' if !mi(wRegIn_Regulations)
			lab var highrest "Hi Regulation"
			keep if in_sample==1
			egen group=group(highrest `v') 
			bys group: egen meanbar=mean(markettobook)
			bys group: egen countbar=count(markettobook)
			bys group: egen sdbar=sd(markettobook)		
			generate hibar = meanbar + invttail(countbar-1,0.025)*(sdbar / sqrt(countbar))
			generate lowbar = meanbar - invttail(countbar-1,0.025)*(sdbar / sqrt(countbar))
			tab group highrest
			tab group `v'
			cap label drop grouplab
			label define grouplab 1 "Hi Reg/No `: variable label `v''" 2 "Hi Reg/ `: variable label `v''" 3 "Lo Reg/No `: variable label `v''" 4 "Lo Reg/ `: variable label `v''"
			label values group grouplab
			lab var meanbar "Market to Book"
			bys group: keep if _n==1
			bys group: su meanbar, d
		}
		twoway (bar  meanbar group, barwidth(.5) ) (rcap hibar lowbar group), scheme(s1mono) xtitle("") ytitle( "Market to Book") legend(off) ///
		xlabel(1 `" "Hi Reg" "No `: variable label `v''" "' 2 `" "Hi Reg" " `: variable label `v''" "' 3 `""Lo Reg" "No `: variable label `v''""' 4 `""Lo Reg" " `: variable label `v''" "', valuelabel) 
		*graph export "$path\bar_`v'.png" , replace
		restore
}



/*Table 6*/

quietly{
eststo clear
sort permno fyear

lab var wmarkup_ebit "Markup"

lab var wPCM_compu "Margin"

eststo: areg wmarkup_ebit  1.highrest##1.connected_maj  l.wbookleverage    l.wtang l.wcapx l.wmarkup_ebit l.wannualret l.wlnsize   i.yr if  bookequity>0 ,  a(ind_dum) cluster(permno)
estadd local yearfe "Yes"


eststo: areg wPCM_compu  1.highrest##1.connected_maj  l.wbookleverage     l.wtang l.wcapx  l.wPCM_compu l.wannualret l.wlnsize   i.yr  if  bookequity>0 ,  a(ind_dum) cluster(permno)
estadd local yearfe "Yes"
}

esttab, obs  t(2) b(2) r2   label   drop(*.yr  _cons) 

/****************************************************************************************/
*Establishement-level analysis
/****************************************************************************************/
/*Tables 1B and 5*/
		
use  "$path\establishments_data.dta", replace



/*Table 1B*/

local sumvars wmeanRegIn_Regulations ln_emp ln_estab   connected_all 
eststo clear
estpost tabstat `sumvars' if insample==1 ,  stat(mean p50 sd p10 p90) columns(statistics)
eststo total


/*Table 5*/
quietly{
eststo clear

eststo: reghdfe ln_estab 1.high_inten#c.connected_all c.connected_all i.stateid , a(ind_year) cluster(id)

eststo: reghdfe ln_estab 1.high_inten#c.connected_maj c.connected_maj i.stateid, a(ind_year) cluster(id)

}
esttab, drop( *.stateid  _cons) b(2) 


/****************************************************************************************/
*Event-level analysis
/****************************************************************************************/

eststo clear
use  "$path\events_data.dta", replace


eststo: reg wCAR connected_all if t==1, robust

eststo: reg wCAR connected_maj if t==1, robust

eststo: reg wCAR connected_all if t==3, robust

eststo: reg wCAR connected_maj if t==3, robust

eststo: reg wCAR connected_all if t==10, robust

eststo: reg wCAR connected_maj if t==10, robust
esttab, r2 
		

